﻿using System;
using System.Reflection;

public class PluginLoader : MarshalByRefObject
{
    public void LoadAndExecutePlugin(string dllPath)
    {
        try
        {
            // 加载插件程序集
            var assembly = System.Reflection.Assembly.LoadFrom(dllPath);
            foreach (var t in assembly.GetTypes())
            {
                Console.WriteLine(t.FullName);
            }
            //使用加载的程序集...
            var type = assembly.GetType("ClassLibrary1.MathOperations");
            var method = type.GetMethod("Add");
            var instance = Activator.CreateInstance(type);
            //var result = instance.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, instance, new object[] { 5, 3 });
            var res = method.Invoke(instance, new object[] { 5, 3 });

            Console.WriteLine($"插件函数已成功调用{res}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"调用插件函数时出错: {ex.Message}");
        }
    }
}